package rule

import "regexp"

func init() {
	Register(&idCardRule{Regexp: regexp.MustCompile(`(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)`)})
}

type idCardRule struct {
	*regexp.Regexp
}

func (i *idCardRule) Name() string {
	return "idCard"
}

func (i *idCardRule) Check(in *RuleInput) (err error) {
	if v, ok := in.Value.(string); ok && v != "" && i.FindStringIndex(v) != nil {
		return
	}
	return in.Error()
}
